/*
 * $Id: ActRequestLoginDetailsDAO.java,v 1.8 2009/10/30 22:39:40 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.database.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.ActRequestLoginDetails;

/**
 * @author 212042764
 * 
 */
public class ActRequestLoginDetailsDAO extends BaseDAO implements
		IRequestLoginDetailsDAO {
	/**
	 * Eclispe generated
	 */
	private static final long serialVersionUID = 6158477409715331279L;

	private static final String TABLE_NAME = "com.ge.healthcare.autosc.common.database.pojo.ActRequestLoginDetails";

	private static final String COL_REQUEST_LOGIN_DETAILS_ID = "requestLoginDetailId";
	private static final String COL_REQUEST_ID = "requestId";
	private static final String COL_NAME = "name";

	public ActRequestLoginDetailsDAO() {
		super(TABLE_NAME);
	}

	public ActRequestLoginDetails getRequestLoginDetailsByID(
			Long requestLoginDetailId) {
		Object request = findObjectByID(COL_REQUEST_LOGIN_DETAILS_ID,
				requestLoginDetailId);
		if (!ASCUtil.isNullOrEmptyObject(request)) {
			return (ActRequestLoginDetails) request;
		}
		return null;
	}

	/**
	 * Returns login details for the specified request id The login details are
	 * returned in a map so that priority user can be looked up easily
	 * 
	 * @param requestLoginDetailId
	 * @return
	 */
	public Map<String, String> getLoginDetailsByRequestID(
			Long requestLoginDetailId) {
		List<?> rtvdLst = findByProperty(COL_REQUEST_ID, requestLoginDetailId);
		if (ASCUtil.isNullOrEmptyList(rtvdLst)) {
			return null;
		}
		Map<String, String> loginDetailsMap = new HashMap<String, String>();
		for (int iCount = 0; iCount < rtvdLst.size(); iCount++) {
			ActRequestLoginDetails loginDetail = (ActRequestLoginDetails) rtvdLst
					.get(iCount);
			loginDetailsMap.put(loginDetail.getName(), loginDetail
					.getPassword());
		}
		return loginDetailsMap;
	}

	/**
	 * Retruns passcode for the specified user
	 * 
	 * @param requestId
	 * @param user
	 * @return
	 */
	public String getLoginDetailsByRequestIDAndUser(Long requestId, String user) {
		Map<String, Object> whereClause = new HashMap<String, Object>();
		whereClause.put(COL_REQUEST_ID, requestId);
		whereClause.put(COL_NAME, user);
		List<?> rtvdList = findByProperties(whereClause);
		if (ASCUtil.isNullOrEmptyList(rtvdList)) {
			return null;
		}
		return ((ActRequestLoginDetails) rtvdList.get(0)).getPassword();
	}

	public ActRequestLoginDetails addRequestLoginDetails(Long requestId,
			String username, String passcode) {
		ActRequestLoginDetails requestLoginDetails = new ActRequestLoginDetails();
		requestLoginDetails.setRequestId(requestId);
		requestLoginDetails.setName(username);
		requestLoginDetails.setPassword(passcode);
		this.save(requestLoginDetails);
		return requestLoginDetails;
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#save(java.lang.Object)
	 */
	public void save(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IRequestTypeDAO#saveAlways(java.lang.Object)
	 */
	public void saveAlways(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#deleteByID(java.lang.Object)
	 */
	public void deleteByID(Object pojo) {
		super.deleteByID(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#refresh(java.lang.Object)
	 */
	public void refresh(Object pojo) {
		super.refresh(pojo);
	}
}
